// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    @property(cc.Sprite)
    solidertouch: cc.Sprite = null;

    @property(cc.Sprite)
    tank: cc.Sprite = null;


    @property(cc.Label)
    checkscore: cc.Label = null;

    @property
    score: number = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //当手指触点落在目标节点区域内时。
        this.solidertouch.node.on('touchstart',this.onTouch,this)

        //当手指在屏幕上移动时。      
        this.solidertouch.node.on('touchmove',this.onTouchMove,this)
        //当手指在目标节点区域内离开屏幕时。
        this.solidertouch.node.on('touchend',this.onTouchEnd,this)
        //当手指在目标节点区域外离开屏幕时。
        this.solidertouch.node.on('touchcancel',this.onTouchCancel,this)


        //
        


        

    }
    onTouch(e : cc.Event.EventTouch){
        //cc.log("点击到 并显示触发位置"+"x值是"+e.getLocationX(),"y值是"+e.getLocationY())
    }
    onTouchMove(e : cc.Event.EventTouch){
        this.solidertouch.node.x=e.getLocationX()
        this.solidertouch.node.y=50
        //this.solidertouch.node.y=e.getLocationY()
        //cc.log("拖拽到 "+"x值是"+e.getLocationX(),"y值是"+e.getLocationY())
    }

    onTouchEnd(e : cc.Event.EventTouch){
        this.solidertouch.node.x=e.getLocationX()
        this.solidertouch.node.y=50
        //this.solidertouch.node.y=e.getLocationY()
        //cc.log("松手了 "+"x值是"+e.getLocationX(),"y值是"+e.getLocationY())
    }

    onTouchCancel(e : cc.Event.EventTouch){
        //cc.log("离开屏幕 "+"x值是"+e.getLocationX(),"y值是"+e.getLocationY())

    }

    start () {

    }

    update (dt) {


        
        
        if(this.tank.node.y < 0){
            this.tank.node.y =650


            this.tank.node.x = Math.random()*897

            if(this.tank.node.x <62.5){
                //随机位置 下坠
                this.tank.node.x = 62.5
                console.log("走到了小于 判断"+this.tank.node.x)

            }
            
            //console.log(this.tank.node.x)

        }

        // 表示 左右滑动体 和 下坠体 碰撞检测
        //235 -185 <= 50   235-285 
        if(this.tank.node.x - this.solidertouch.node.x <= 110 && Math.abs(this.tank.node.x - this.solidertouch.node.x) <= 110 &&this.tank.node.y<= 166 == true){
            

            //console.log("下坠体x 值 是 "+this.tank.node.x + "滑动体x 值 是" + this.solidertouch.node.x)
            //console.log("下坠体y 值 是 "+this.tank.node.y)
            this.tank.node.y =700
            this.score = this.score + 1 
            
            console.log("得分累计 "+this.score)
            this.checkscore.string = "得分 "+this.score.toString()
            

            this.tank.node.x = Math.random()*897

            if(this.tank.node.x <62.5){
                //随机位置 下坠
                this.tank.node.x = 62.5
                console.log("走到了小于 判断"+this.tank.node.x)

            }
            
            //console.log(this.tank.node.x)
                
            
            

        }

        //游戏难度 调整

        //特难
        //this.tank.node.y = this.tank.node.y - 10
        //this.tank.node.y = this.tank.node.y - 10
        this.tank.node.y = this.tank.node.y - 0.1


    }
}
